
#include "flashwr.h"

	  

#define FLASH_USER_START_ADDR   ADDR_FLASH_PAGE_40   /* Start @ of user Flash area */
#define FLASH_USER_END_ADDR     (ADDR_FLASH_PAGE_43 + FLASH_PAGE_SIZE - 1)   /* End @ of user Flash area */

#define DATA_32                 ((uint32_t)0x12345678)
#define DATA_64                 ((uint64_t)0x123456ab123456ac)

union data64toch
{
	uint64_t data_64;
	uint16_t data_16[2];
	uint8_t ch[8];
} data_to_ch;
/* USER CODE BEGIN PV */
uint32_t FirstPage = 0, NbOfPages = 0;
uint32_t Address = 0, PageError = 0;
__IO uint32_t MemoryProgramStatus = 0;
__IO uint32_t data32 = 0;
__IO uint64_t data64 = 0;
/*Variable used for Erase procedure*/
static FLASH_EraseInitTypeDef EraseInitStruct;
 uint32_t GetPage(uint32_t Addr);
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
static uint32_t GetPage(uint32_t Address);
void flash_set_data(uint16_t id)
{
 /* Unlock the Flash to enable the flash control register access *************/
  HAL_FLASH_Unlock();
  uint64_t advv;
 /* Clear OPTVERR bit set on virgin samples */
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);

  /* Erase the user Flash area
    (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/

  /* Get the 1st page to erase */
  FirstPage =GetPage(FLASH_USER_START_ADDR);

  /* Get the number of pages to erase from 1st page */
  NbOfPages = GetPage(FLASH_USER_END_ADDR) - FirstPage + 1;

  /* Fill EraseInit structure*/
  EraseInitStruct.TypeErase   = FLASH_TYPEERASE_PAGES;
  EraseInitStruct.Page        = FirstPage;
  EraseInitStruct.NbPages     = NbOfPages;

  /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
     you have to make sure that these data are rewritten before they are accessed during code
     execution. If this cannot be done safely, it is recommended to flush the caches by setting the
     DCRST and ICRST bits in the FLASH_CR register. */
  if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
  {
      HAL_Delay(1);
  }
	Address = FLASH_USER_START_ADDR;

 // while (Address < FLASH_USER_END_ADDR)
 // {
	  data_to_ch.data_16[0]=id;
		data_to_ch.data_16[1]=id;
	  data64=data_to_ch.data_64;
    if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address,data64) == HAL_OK)
	//if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address,DATA_64) == HAL_OK)
    {
			HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address+8,DATA_64);
      HAL_Delay(1);
    }
   else
    {
      /* Error occurred while writing data in Flash memory.
         User can add here some code to deal with this error */
       HAL_Delay(1);
    }
 HAL_FLASH_Lock();
}

uint16_t flash_get_id(void)
{
	  /* Check if the programmed data is OK
      MemoryProgramStatus = 0: data programmed correctly
      MemoryProgramStatus != 0: number of words not programmed correctly ******/
	uint16_t id;
  Address = FLASH_USER_START_ADDR;
  MemoryProgramStatus = 0x0;


    data32 = *(__IO uint32_t *)Address;
		
   
  id=data32;

  return id;

}

 uint32_t GetPage(uint32_t Addr)
{
  return (Addr - FLASH_BASE) / FLASH_PAGE_SIZE;;
}
